<template>
  <div class="leftTree">
    <el-input v-model="filterText" placeholder="输入社区名称搜索" class="mb-10"></el-input>
    <el-tree
      ref="tree"
      :props="props"
      :data="data"
      :expand-on-click-node="false"
      :filter-node-method="filterNode"
      @node-click="handleNodeClick"
    >
    </el-tree>
  </div>
</template>
<script>
import { queryList } from '@/api/dept'
import { mapMutations } from 'vuex'
export default {
  name: 'leftTree',
  components: {},
  data () {
    return {
      filterText: '',
      props: {
        label: 'name',
        isLeaf: 'leaf'
      },
      data: []
    }
  },
  // 计算属性
  computed: {
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    }
  },
  mounted () {
    this.loadNode()
  },
  methods: {
    ...mapMutations('system', ['SET_DEPT_ID']),
    handleNodeClick (data) {
      this.SET_DEPT_ID(data.id)
    },
    async loadNode () {
      const data = await queryList()
      this.data = data
    },
    filterNode (value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    }
  }
}
</script>
<style lang="scss" scoped>
.leftTree {
  margin-right: 10px;
}
.mb-10 {
  margin-bottom: 10px;
}
</style>
